package com.ay.wiki_plt.service.Impl;


import com.ay.wiki_plt.common.PageBean;
import com.ay.wiki_plt.domain.Category;
import com.ay.wiki_plt.mapper.CategoryMapper;
import com.ay.wiki_plt.service.CategoryService;
import com.ay.wiki_plt.utils.SnowFlake;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SnowFlake snowFlake;

    /*查询全部分类*/
    @Override
    public PageBean list(Integer page, Integer pageSize, String name) {
        // 开启PageHelper插件
        PageHelper.startPage(page, pageSize);
        List<Category> ebookList = categoryMapper.list(name);
        /*封装pagebean对象*/
        Page<Category> pageinfo = (Page<Category>) ebookList;
        return new PageBean(pageinfo.getTotal(), pageinfo.getResult());
    }

    /*分类保存和修改接口*/
    @Override
    public void save(Category category) {
        Long categoryId = category.getId();
        if(categoryId ==null){
            //新增分类
            category.setId(snowFlake.nextId());
            categoryMapper.saveCategory(category);
        } else{
            //修改分类
            categoryMapper.updateCategory(category);
        }
    }

    /*电子书删除接口*/
    @Override
    public void delete(List<Long> ids) {
        categoryMapper.deleteByCategoryList(ids);
    }

    /*查询全部分类*/
    @Override
    public List<Category> getCategoryList() {
        return categoryMapper.getCategoryList();
    }


    /*根据id查询电子书*/
    @Override
    public Category listOne(Integer id) {
        return categoryMapper.getByIdCategory(id);
    }




}
